package cn.tedu.wordcount;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * 驱动类
 * @author waqwb
 *
 */
public class WordCountDriver {
	public static void main(String[] args) throws Exception {
		//1.加载配置
		Configuration conf = new Configuration();
		//2.向yarn申请job
		Job job = Job.getInstance();
		//3.设置启动的入口类
		job.setJarByClass(WordCountDriver.class);
		//4.设置Mapper类
		job.setMapperClass(WordCountMapper.class);
		//5.设置Reducer类
		job.setReducerClass(WordCountReducer.class);

		//6.设置Reducer的输出类型
		//如果Mapper和Redcuer的输出类型一致
		//可以只设置一次
		/*
		 * job.setMapOutputKeyClass(Text.class);
		 * job.setMapOutputValueClass(IntWritable.class);
		 */
		job.setOutputKeyClass(Text.class);//字符
		job.setOutputValueClass(IntWritable.class);//次数

		//7.指定输入的文件
		//如果指定的只是文件，那么就处理这个文件
		//如果指定的是目录，那么会处理这个目录下所有的文件
		Path pathInput = new Path("hdfs://hadoop1:9000/txt/words.txt");
		FileInputFormat.addInputPath(job, pathInput);
		//8.指定输出的路径
		//输出路径一定要不存在
		Path pathOutput = new Path("hdfs://hadoop1:9000/result/word_count");
		FileOutputFormat.setOutputPath(job, pathOutput);
		//9.开始执行任务
		job.waitForCompletion(true);
	}
}




